package com.ss.android.article.base.feature.subscribe.b;

import android.content.Context;
import android.os.Handler;

import com.bytedance.article.common.c.TTUtils;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.UrlBuilder;
import java.util.ArrayList;
import org.json.JSONObject;

final class b extends AbsApiThread {
    private final Context a;
    private final Handler b;
    private j c;

    public b(Context arg7, Handler arg8, int arg9, int arg10, String arg11, boolean arg12, boolean arg13) {
        super("SubscribeListThread");
        this.a = arg7.getApplicationContext();
        this.b = arg8;
        this.c = new j(arg9, arg10, arg11, arg12, arg13);
    }

    private static int a(Context arg6, j arg7) {
        int v0 = 18;
        int v2 = 1;
        try {
            UrlBuilder v3 = new UrlBuilder(ArticleConstants.entrySubscriptionListUrlV1);
            String v4 = "req_type";
            if(arg7.b <= 1) {
                v2 = arg7.b;
            }

            v3.addParam(v4, v2);
            if(!android.support.design.a.isTextEmpty(arg7.c)) {
                v3.addParam("version", arg7.c);
            }

            if(arg7.f) {
                v3.addParam("has_tip_new", 1);
            }

            String v2_1 = NetworkUtils.doGet(204800, v3.build());
            if(v2_1 == null) {
                return v0;
            }

            if(v2_1.length() == 0) {
                return v0;
            }

            JSONObject v3_1 = new JSONObject(v2_1);
            if(!isApiSuccess(v3_1)) {
                return v0;
            }

            JSONObject v0_2 = v3_1.optJSONObject("data");
            if(v0_2 != null) {
                arg7.i.addAll(android.support.design.a.a(v0_2.optJSONArray("data")));
                arg7.j = v0_2.optString("version");
            }

            arg7.l = optBoolean(v0_2, "tip_new", false);
            arg7.k = optBoolean(v0_2, "refresh", false);
            if(arg7.k) {
                DBHelper.getInstance(arg6).insertSubscribeList(arg7.i, arg7.j);
            }
        }
        catch(Exception v0_1) {
            return TTUtils.getErrorCode(arg6, ((Throwable)v0_1));
        }

        return 0;

    }

    public final void run() {
        j v1;
        int v7 = 2;
        int v4 = 4;
        if(this.c.b == v4) {
            DBHelper.getInstance(this.a).clearSubscribe();
        }

        String v0 = null;
        if(this.c.d) {
            v1 = new j(this.c);
            v1.i = new ArrayList();
            Context v0_1 = this.a;
            String[] v2 = new String[1];
            DBHelper.getInstance(v0_1).a(v1.i, v2);
            v1.j = v2[0];
            v1.g = 0;
            v1.h = true;
            v0 = v1.j;
            this.b.obtainMessage(v4, v1.g, 1, v1).sendToTarget();
        }

        if(this.c.e) {
            v1 = new j(this.c);
            if(!android.support.design.a.isTextEmpty(v0) && (android.support.design.a.isTextEmpty(v1.c))) {
                v1.c = v0;
            }

            if(!NetworkUtils.isNetworkAvailable(this.a)) {
                v1.g = 12;
                this.b.obtainMessage(v4, v1.g, v7, v1).sendToTarget();
                return;
            }

            v1.i = new ArrayList();
            v1.g = a(this.a, v1);
            v1.h = false;
            this.b.obtainMessage(v4, v1.g, v7, v1).sendToTarget();
        }
    }
}

